package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wwy
 * Date: 2024-09-03
 * Time: 22:57
 */
public class AddOperation implements IOperation{
    @Override
    public  void work(BookList bookList){
        System.out.println("增加图书");
        //判满
        int currentSize=bookList.getUsedSize();
        if (currentSize == bookList.getBooks().length) {
            return;
        }
        //构建对象
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入新增的书名:");
        String name= scanner.nextLine();
        System.out.println("请输入新增的书名的作者:");
        String author= scanner.nextLine();
        System.out.println("请输入新增的书名的价格:");
        int price= scanner.nextInt();
        System.out.println(scanner.nextLine());
        System.out.println("请输入新增的书名的类型");
        String type= scanner.nextLine();
        Book newBook=new Book(name,author,price,type);
        //判断有没有
        for (int i = 0; i <currentSize; i++) {
            Book book=bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println("有这本书不能插入:");
                System.out.println(book);
                return;
            }
        }
        //放
        bookList.setBook(currentSize,newBook);
        bookList.setUsedSize(currentSize+1);
        System.out.println("新增图书成功");
    }
}
